package org.csu.mypetstore.web.servlets;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UsernameExistServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        AccountService accountService = new AccountService();
        Account rs = accountService.getAccount(username);

        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();

        Result result = new Result();

        if(rs == null){
            //用户名可用
            result.setCode(0);
            result.setMsg("Not Exist");
        }
        else {
            //用户名不可用
            result.setCode(1);
            result.setMsg("Exist");
        }

        String rt = JSON.toJSONString(result);
        out.print(rt);

        out.flush();
        out.close();
    }
}
